/**
 * Copyright &copy; 2012-2016 civet All rights reserved.
 */
package cn.mcsys.myadmin.modules.air.web;

import cn.mcsys.myadmin.common.config.Global;
import cn.mcsys.myadmin.common.persistence.Page;
import cn.mcsys.myadmin.common.web.BaseController;
import cn.mcsys.myadmin.common.web.JsonResult;
import cn.mcsys.myadmin.modules.air.entity.AirCar;
import cn.mcsys.myadmin.modules.air.entity.AirDriver;
import cn.mcsys.myadmin.modules.air.entity.AirRidePoint;
import cn.mcsys.myadmin.modules.air.service.AirCarService;
import cn.mcsys.myadmin.modules.air.service.AirDriverService;
import cn.mcsys.myadmin.modules.air.service.AirRidePointService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 司机管理Controller
 * @author leopold
 * @version 2018-08-13
 */
@Controller
@RequestMapping(value = "${adminPath}/air/airDriver")
public class AirDriverController extends BaseController {

	@Autowired
	private AirDriverService airDriverService;

	@Autowired
	private AirCarService airCarService;

	@Autowired
	private AirRidePointService airRidePointService;
	
	@RequiresPermissions("air:airDriver:view")
	@RequestMapping(value = {"list", ""})
	public String list(AirDriver airDriver, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AirDriver> page = airDriverService.findPage(new Page<AirDriver>(request, response), airDriver);
//		airDriverService.changeIdNumber(page);
		List<AirRidePoint> rides = airRidePointService.findAllList();
		model.addAttribute("page", page);
		model.addAttribute("rides",rides);
		return "modules/air/airDriverList";
	}

	@RequiresPermissions("air:airDriver:view")
	@RequestMapping(value = "form")
	public String form(AirDriver airDriver, Model model) {
	    if (airDriver.getId() != null){
            airDriver = airDriverService.get(airDriver.getId());
        }
		List<AirCar> cars = airCarService.findAllList(new AirCar());
		model.addAttribute("airDriver", airDriver);
		model.addAttribute("cars",cars);
		return "modules/air/airDriverForm";
	}

	@RequiresPermissions("air:airDriver:edit")
	@RequestMapping(value = "save")
	public String save(AirDriver airDriver, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, airDriver)){
			return form(airDriver, model);
		}
		airDriverService.save(airDriver);
//		addMessage(redirectAttributes, "保存司机成功");
		return "redirect:"+Global.getAdminPath()+"/air/airDriver/?repage";
	}
	
	@RequiresPermissions("air:airDriver:edit")
	@RequestMapping(value = "delete")
	public String delete(AirDriver airDriver, RedirectAttributes redirectAttributes) {
		airDriverService.delete(airDriver);
//		addMessage(redirectAttributes, "删除司机成功");
		return "redirect:"+Global.getAdminPath()+"/air/airDriver/?repage";
	}

	@RequestMapping(value = "getDriver")
	@ResponseBody
	public JsonResult getDriver(Long driverId){
		AirDriver airDriver = airDriverService.get(driverId.intValue());
		return JsonResult.success(airDriver);
	}

}